package com.example.combwx.test;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import com.blankj.utilcode.util.BarUtils;
import com.example.combwx.R;
import com.example.combwx.mvp.view.livestreaming.LiveStreamingFragment;
import com.example.combwx.mvp.view.news.NewsFragment;
import com.example.combwx.mvp.view.people.PeopleFragment;
import com.example.combwx.mvp.view.uesr.UserFragment;
import com.example.combwx.mvp.view.video.VideoFragment;
import com.example.combwx.widget.BottomButton;

public class BottomButtonActivity extends AppCompatActivity implements BottomButton.SelectListener {

    private BottomButton bottom1;
    private BottomButton bottom3;
    private BottomButton bottom5;
    private BottomButton bottom7;
    private BottomButton bottom9;
    private LinearLayout buttonDa;

    private UserFragment userFragment;
    private NewsFragment newsFragment;
    private PeopleFragment peopleFragment;
    private VideoFragment videoFragment;
    private LiveStreamingFragment liveStreamingFragment;
    private FragmentTransaction fragmentTransaction;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bottom_button);
        getStatusColor(false);
        initView();
        bottom1.setContent("新闻").setNomalColor(Color.BLACK)
                .setSelColor(Color.RED)
                .setNomalImg(R.drawable.tu1_f)
                .setSelImg(R.drawable.tu1_t)
                .setListener(this::onSelect)
                .setShowPoint(false);
        bottom3.setContent("人民号").setNomalColor(Color.BLACK)
                .setSelColor(Color.RED)
                .setNomalImg(R.drawable.tu2_f)
                .setSelImg(R.drawable.tu2_t)
                .setListener(this::onSelect)
                .setShowPoint(false);
        bottom5.setContent("视频").setNomalColor(Color.BLACK)
                .setSelColor(Color.RED)
                .setNomalImg(R.drawable.tu3_f)
                .setSelImg(R.drawable.tu3_t)
                .setListener(this::onSelect)
                .setShowPoint(false);
        bottom7.setContent("直播").setNomalColor(Color.BLACK)
                .setSelColor(Color.RED)
                .setNomalImg(R.drawable.tu4_f)
                .setSelImg(R.drawable.tu4_t)
                .setListener(this::onSelect)
                .setShowPoint(false);
        bottom9.setContent("我的").setNomalColor(Color.BLACK)
                .setSelColor(Color.RED)
                .setNomalImg(R.drawable.tu5_f)
                .setSelImg(R.drawable.tu5_t)
                .setListener(this::onSelect)
                .setShowPoint(false);

        fragmentTransaction = getSupportFragmentManager().beginTransaction();
        newsFragment = new NewsFragment();
        fragmentTransaction.add(R.id.button_da, newsFragment);
        fragmentTransaction.commit();
    }

    public void Head() {
        fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (newsFragment != null) {
            fragmentTransaction.hide(newsFragment);
        }
        if (peopleFragment != null) {
            fragmentTransaction.hide(peopleFragment);
        }
        if (videoFragment != null) {
            fragmentTransaction.hide(videoFragment);
        }
        if (liveStreamingFragment != null) {
            fragmentTransaction.hide(liveStreamingFragment);
        }
        if (userFragment != null) {
            fragmentTransaction.hide(userFragment);
        }
        fragmentTransaction.commit();
    }

    public void showf(int id) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        switch (id) {
            case R.id.bottom1:
                fragmentTransaction.show(newsFragment);
                break;
            case R.id.bottom3:
                if (peopleFragment==null){
                    peopleFragment = new PeopleFragment();
                    fragmentTransaction.add(R.id.button_da, peopleFragment);
                }else {
                    fragmentTransaction.show(peopleFragment);
                }
                break;
            case R.id.bottom5:
                if (videoFragment==null){
                    videoFragment = new VideoFragment();
                    fragmentTransaction.add(R.id.button_da, videoFragment);
                }else {
                    fragmentTransaction.show(videoFragment);
                }
                break;
            case R.id.bottom7:
                if (liveStreamingFragment==null){
                    liveStreamingFragment = new LiveStreamingFragment();
                    fragmentTransaction.add(R.id.button_da, liveStreamingFragment);
                }else {
                    fragmentTransaction.show(liveStreamingFragment);
                }
                break;
            case R.id.bottom9:
                if (userFragment==null){
                    userFragment = new UserFragment();
                    fragmentTransaction.add(R.id.button_da, userFragment);
                }else {
                    fragmentTransaction.show(userFragment);
                }
                break;
        }
        fragmentTransaction.commit();
    }

    @Override
    public void onSelect(int id) {

        if (id == R.id.bottom1) {
            bottom5.clearSelect();
            bottom7.clearSelect();
            bottom9.clearSelect();
            bottom3.clearSelect();
            Head();
            showf(R.id.bottom1);
        } else if (id == R.id.bottom3) {
            bottom5.clearSelect();
            bottom7.clearSelect();
            bottom9.clearSelect();
            bottom1.clearSelect();
           Head();
           showf(R.id.bottom3);

        } else if (id == R.id.bottom5) {
            bottom3.clearSelect();
            bottom7.clearSelect();
            bottom9.clearSelect();
            bottom1.clearSelect();
            Head();
            showf(R.id.bottom5);
        } else if (id == R.id.bottom7) {
            bottom3.clearSelect();
            bottom5.clearSelect();
            bottom9.clearSelect();
            bottom1.clearSelect();
            Head();
            showf(R.id.bottom7);
        } else if (id == R.id.bottom9) {
            bottom3.clearSelect();
            bottom7.clearSelect();
            bottom5.clearSelect();
            bottom1.clearSelect();
            Head();
            showf(R.id.bottom9);
        }
    }

    private void initView() {
        bottom1 = (BottomButton) findViewById(R.id.bottom1);
        bottom3 = (BottomButton) findViewById(R.id.bottom3);
        bottom5 = (BottomButton) findViewById(R.id.bottom5);
        bottom7 = (BottomButton) findViewById(R.id.bottom7);
        bottom9 = (BottomButton) findViewById(R.id.bottom9);
        buttonDa = (LinearLayout) findViewById(R.id.button_da);
    }
    public void getStatusColor(boolean is){
        if (is==true){
            BarUtils.setStatusBarColor(this, Color.RED);
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        }else {
            BarUtils.setStatusBarColor(this, Color.WHITE);
            getWindow().getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
    }

}
